//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    partial class Row
    {
        internal class RowAttributes
        {
            public ReadContext ReadAttribute(ReadContext context, int previousIndex, StyleRecordManager manager)
            {
                ReadContext c = context.Read("row");
                {
                    int? i = c.GetOptionalInteger("r");
                    if (i != null)
                        Index = i.Value - 1;
                    else
                        Index = previousIndex + 1;
                    string ht = c.GetOptionalString("ht");
                    if (ht != null)
                        _height = PointsToPixelConverter.ToPixel(double.Parse(ht, CultureInfo.InvariantCulture));
                    _customHeight = c.GetOptionalBoolean("customHeight", false);
                    string style = c.GetOptionalString("s");
                    if (style != null && c.GetOptionalBoolean("customFormat", false))
                        _formattingRecord = manager[int.Parse(style)];
                    _hidden = c.GetOptionalBoolean("hidden", false);
                    _outlineLevel = (byte)c.GetOptionalInteger("outlineLevel", 0);
                    _collapsed = c.GetOptionalBoolean("collapsed", false);
                    _thickBot = c.GetOptionalBoolean("thickBot", false);
                    _thickTop = c.GetOptionalBoolean("thickTop", false);
                    _ph = c.GetOptionalBoolean("ph", false);
                }
                return c;
            }
            public int Index;
            double? _height;
            private bool _customHeight;
            bool _hidden;
            byte _outlineLevel;
            bool _collapsed;
            bool _thickBot;
            bool _thickTop;
            bool _ph;
            CellFormattingRecord _formattingRecord;
            public void Apply(Row r, GroupBuilderStack<Row> groupBuilder)
            {
                r._height = _height;
                r.CustomHeight = _customHeight;
                r._hidden = _hidden;
                groupBuilder.ApplyOutlineLevel(r, r, _outlineLevel, !this._hidden,  _collapsed);
                //r.collapsed = collapsed;
                r.thickBot = _thickBot;
                r.thickTop = _thickTop;
                r._ph = _ph;
                if (FormattingRecord != null)
                    r.SetStyleRecordSafe(this.FormattingRecord);
            }

            public CellFormattingRecord FormattingRecord
            {
                get { return _formattingRecord; }
            }
        }

    }
}
